package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.cust.ContextCustDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;

@Service
public class UserRegionCommonAttrHandler extends AbstractAttrHandler {

    @Override
    public void init(CommonAttrVO commonAttr) {
        List<String> values = new ArrayList<>();
        if (KiteStringUtils.isNotEmpty(commonAttr.getValue())) {
            values = Arrays.asList(commonAttr.getValue().split(SymbolConsts.COMMA));
        }

        if(values.size() < 3) {
            ContextCustDTO custDTO = KiteWebContext.getContext().getCust();
            CuccContextCustDTO cuccContextCustDTO = KiteBeanUtils.transform(custDTO, CuccContextCustDTO.class);
            if (Objects.nonNull(cuccContextCustDTO)) {
                List<String> regionCodes = new ArrayList<>();
                if (KiteStringUtils.isNotEmpty(cuccContextCustDTO.getProvinceCode())) {
                    regionCodes.add(cuccContextCustDTO.getProvinceCode());

                    if (KiteStringUtils.isNotEmpty(cuccContextCustDTO.getEparchyCode()) || KiteStringUtils.isNotEmpty(cuccContextCustDTO.getLanId())) {
                        regionCodes.add(KiteStringUtils.isNotEmpty(cuccContextCustDTO.getEparchyCode())?cuccContextCustDTO.getEparchyCode():cuccContextCustDTO.getLanId());

                        if (KiteStringUtils.isNotEmpty(cuccContextCustDTO.getRegionId())) {
                            regionCodes.add(cuccContextCustDTO.getRegionId());
                        }
                    }
                }

                // value省市区不全，且与客户对象获取的省市区前缀相同，则取客户省市区补全属性值
                commonAttr.setValue(KiteStringUtils.join(regionCodes, SymbolConsts.COMMA));
                commonAttr.setValueDesc(LocalAttrHelper.getRegionNames(regionCodes, SymbolConsts.HYPHEN));
                setCommonAttr(commonAttr);
            }
        }
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        if (KiteStringUtils.isEmpty(value)) {
            valueDesc = "";
        } else {
            List<String> regionCodes = Arrays.asList(KiteStringUtils.split(value, SymbolConsts.COMMA));
            valueDesc = LocalAttrHelper.getRegionNames(regionCodes, SymbolConsts.HYPHEN);
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
